package org.fintool.runners;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;

import org.fintool.beans.Scrip;
import org.fintool.beans.StockInfoMaster;
import org.fintool.beans.Tick;
import org.fintool.common.MathUtil;
import org.fintool.common.StockMasterUtil;
import org.fintool.functional.FinanceUtilDay;

/**
 * @author pirate
 *If a stock opens positive what is the max gain it would give
 */
public class FindoverPreviousDay {

	static double minimumMove=.1;
	static double gainNeeded=0.3;



	public static void main(String[] args) {

		System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.16 Safari/534.24");

		ArrayList alAsxList=null;

		alAsxList=StockMasterUtil.asxStocks;



		StockInfoMaster stockMaster=null;

		FinanceUtilDay fd=new FinanceUtilDay();



		String currString=null;

		int countScripNetPostiveOpens;
		int countScripResonableGain;

		int countScripNetNegativeOpens;
		int countScripResonableLoss;

		try{

			BufferedWriter bwriter=new BufferedWriter(new FileWriter("D:\\fin_base\\asx_prevday_gain.txt"));

		for(int i=0;i<alAsxList.size();i++){

			try{
			stockMaster=(StockInfoMaster)alAsxList.get(i);
           try{

			//fd.updateQuotes(stockMaster.getName().toUpperCase(),stockMaster.getExchange().toUpperCase());

           }catch (Exception e) {
			// TODO: handle exception
		}

           if(!stockMaster.getName().equalsIgnoreCase("BHP")){
        	  // continue;
           }else{
        	   //fd.updateQuotes(stockMaster.getName().toUpperCase(),stockMaster.getExchange().toUpperCase());
           }

			Scrip scrip=fd.getScripData(stockMaster.getName().toUpperCase(),stockMaster.getExchange().toUpperCase());

			ArrayList tickerList=scrip.getTickers();
			Tick currentTicker=null;

			countScripNetPostiveOpens=0;
			 countScripResonableGain=0;

			 countScripNetNegativeOpens=0;
			 countScripResonableLoss=0;


			try{


				//Current ticker /per ticker
				for(int tickCount=0;tickCount<tickerList.size();tickCount++ ){
					currentTicker=(Tick)tickerList.get(tickCount);


                    System.out.println(currentTicker.getOpen()+","+currentTicker.getHigh()+" and "+currentTicker.getPercentageDiffLastMaxVsOpen());
					if(currentTicker.getPercentageDiffAtOpen()>0.0){
					if(currentTicker.getPercentageDiffLastMaxVsOpen()>minimumMove){

						countScripNetPostiveOpens+=1;
						if(currentTicker.getPercentageDiffOpenMax()>(gainNeeded)){
						     countScripResonableGain+=1;
						}


					}
					}
					if(currentTicker.getPercentageDiffAtOpen()<(0.0)){
					if(currentTicker.getPercentageDiffLastLowVsOpen()<-(minimumMove)){

						countScripNetNegativeOpens+=1;
						if(currentTicker.getPercentageDiffOpenLow()<-(gainNeeded)){
						     countScripResonableLoss+=1;
						}


					}
					}

				}//Current ticker /per ticker

				currString=scrip.getName()+" - Total positive Gains : "+countScripNetPostiveOpens+" coverted Gain : "+
				countScripResonableGain
				+", Total Negative Opens : "+countScripNetNegativeOpens
				+" coverted Loss :"+countScripResonableLoss
				+" -%age-"
				+MathUtil.getPortion(countScripResonableGain, countScripNetPostiveOpens)
				+"/"
				+MathUtil.getPortion(countScripResonableLoss, countScripNetNegativeOpens);

				if(countScripNetNegativeOpens==0||countScripNetPostiveOpens==0){
					currString="";
				}

				if(MathUtil.getPortion(countScripResonableGain, countScripNetPostiveOpens)<90.0
						&&MathUtil.getPortion(countScripResonableLoss, countScripNetNegativeOpens)<90.0){
					currString="";

				}



			}catch (Exception e) {
				// TODO: handle exception
			}

			if(currString.length()>0){
			System.out.println(currString);
			bwriter.write(currString+"\r\n");
			}
		}//
			catch (Exception e) {
				// TODO: handle exception
			}
		}

		bwriter.close();
	}

	catch (Exception e) {
		e.printStackTrace();
		// TODO: handle exception
	}

	}

}
